﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core.Lister;
using COFE.Core;

namespace COFE.Core.Entry
{
    /// <summary>
    /// Reside in COFEShared, contain a list of SecondaryPropertyProvider factories.
    /// </summary>
    public interface ISecondaryPropertyProviderManager
    {
        /// <summary>
        /// Return a list of property provider factories, which generate secondary property providers.
        /// </summary>
        List<ISecondaryPropertyProvider<T>> GetSecondaryPropertyProviders<T>();

        void Register<T>(ISecondaryPropertyProvider<T> propProvider);
    }

    public static class ISecondaryPropertyProviderManagerExtension
    {
        public static object[] GetSupportedProperty<T>(this ISecondaryPropertyProviderManager propManager, T sourceObj)
        {
            List<object> retVal = new List<object>();

            foreach (var propProvider in propManager.GetSecondaryPropertyProviders<T>())
                retVal.AddRange(propProvider.GetSupportedProperty(sourceObj));

            return retVal.Distinct().ToArray();
        }

        public static PropertyPair GetProperty<T>(this ISecondaryPropertyProviderManager propManager, T sourceObj, object property)
        {
            List<object> retVal = new List<object>();

            foreach (var propProvider in propManager.GetSecondaryPropertyProviders<T>())
                if (propProvider.IsPropertySupported(sourceObj, property))
                    return propProvider.GetProperty(sourceObj, property);

            return null;
        }
    }
}
